package tq.com.tqteacher.adapt;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;

import tq.com.tqcom.net.TQPictureUpload;
import tq.com.tqcom.vh.CommonViewHolder;
import tq.com.tqteacher.R;
import tq.com.tqteacher.model.TQUserInfo;

public class TQLookerDetailAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private ArrayList<TQUserInfo> mTQUserInfos = new ArrayList<>();

    public TQLookerDetailAdapter(Context pContext){
        mContext = pContext;
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup pViewGroup, int pI) {
        return CommonViewHolder.sCommonViewHolder(mContext, R.layout.vh_looker_item_layout, pViewGroup)
                .setHolderBinder(new CommonViewHolder.IViewHolder() {
                    @Override
                    public void bind(View pView, Object data) {
                        if(data == null || !(data instanceof TQUserInfo)) return;
                        TQUserInfo lTQUserInfo = (TQUserInfo)data;

                        ImageView header_icon = pView.findViewById(R.id.header_icon);
                        TextView  user_name= pView.findViewById(R.id.user_name);

                        String url = TQPictureUpload.downloadPath(lTQUserInfo.getPic());
                        RequestOptions lOptions = new RequestOptions().error(R.drawable.tx3)
                                .placeholder(R.drawable.tx4);
                        Glide.with(mContext).load(url).apply(lOptions).into(header_icon);

                        user_name.setText(lTQUserInfo.getName());
                    }
                });
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder pViewHolder, int pI) {
        if(pViewHolder instanceof CommonViewHolder){
            TQUserInfo lUserInfo = mTQUserInfos.get(pI);
            ((CommonViewHolder)pViewHolder).bindData(lUserInfo);
        }
    }

    @Override
    public int getItemCount() {
        return mTQUserInfos.size();
    }


    public void clear(){
        mTQUserInfos.clear();
        notifyDataSetChanged();
    }

    public void append(ArrayList<TQUserInfo> pTQUserInfos){
        int all = mTQUserInfos.size();
        mTQUserInfos.addAll(pTQUserInfos);
        notifyItemRangeInserted(all, pTQUserInfos.size());
    }
}
